package com.example.smeng.xiaowen;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private Bitmap bitmap;
    private ArrayList<File> arrayList = new ArrayList<>();
    private TranslateAnimation mShowAction;
    private ImageView back;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mShowAction.setDuration(500);

        imageView = (ImageView) findViewById(R.id.imageview);
//        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        back = (ImageView) findViewById(R.id.backImage);

        try {
            File file = new File(Environment.getExternalStorageDirectory() + "/zhaobiao/");
            File[] subFile = file.listFiles();

            for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
                // 判断是否为文件夹
                if (!subFile[iFileLength].isDirectory()) {
                    arrayList.add(subFile[iFileLength]);
                }
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        Collections.sort(arrayList, new FileComparator3());
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(i >= arrayList.size()) {
                    i=0;
                    return;
                }
                bitmap = BitmapFactory.decodeFile(arrayList.get(i).toString());
                imageView.setImageBitmap(bitmap);
                mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
                        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                        0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
                mShowAction.setDuration(500);
//                imageView.setAnimation(mShowAction);
//                imageView.startAnimation(mShowAction);
                imageView.setVisibility(View.VISIBLE);
                i++;
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(i >= arrayList.size()) {
                    i=0;
                    return;
                }
                bitmap = BitmapFactory.decodeFile(arrayList.get(i).toString());
                imageView.setImageBitmap(bitmap);
                mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f,
                        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                        0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
                mShowAction.setDuration(500);
//                imageView.setAnimation(mShowAction);
//                imageView.startAnimation(mShowAction);
                imageView.setVisibility(View.VISIBLE);
                i++;
            }
        });



    }
}
